using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FrogControl : EnemiesController
{

   // private Animator anim;
    private Rigidbody2D rb;

    public Transform leftPoint,rightPoint;
    private float leftX,rightX;
    public Collider2D coll;
    public LayerMask ground;

    private bool FaceLeft = true;
    public float Speed,JumpForce;

    // Start is called before the first frame update
    protected override void  Start()
    {
        base.Start();
        rb = GetComponent<Rigidbody2D>();
        //anim = GetComponent<Animator>();
        // transform.DetachChildren();
        leftX = leftPoint.position.x;
        rightX = rightPoint.position.x;
        Destroy(leftPoint.gameObject);
        Destroy(rightPoint.gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        SwitchAnim();
    }

    void Movement(){
        if(FaceLeft){
            if(coll.IsTouchingLayers(ground)){
                anim.SetBool("jumping",true);
                rb.velocity = new Vector2(-Speed,JumpForce);
            }
            if(transform.position.x < leftX){
                rb.velocity = new Vector2(0,0);
                transform.localScale = new Vector3(-1,1,1);
                FaceLeft = false;
            }
        }else{
            if(coll.IsTouchingLayers(ground)){
                anim.SetBool("jumping",true);
                rb.velocity = new Vector2(Speed,JumpForce);
            }
            if(transform.position.x > rightX){
                rb.velocity = new Vector2(0,0);
                transform.localScale = new Vector3(1,1,1);
                FaceLeft = true;
            }  
        }
    }

    void SwitchAnim(){
        if(anim.GetBool("jumping")){
            if(rb.velocity.y < 0.1f){
                anim.SetBool("jumping",false);
                anim.SetBool("failing",true);
            }
        }
        if(coll.IsTouchingLayers(ground) && anim.GetBool("failing")){
            anim.SetBool("failing",false);
        }
    }

}
